<template>
  <div class="user-management">
    <!-- 顶部彩色圆点 -->
    <div class="status-dots">
      <span class="badge-dot bg-red"></span>
      <span class="badge-dot bg-yellow"></span>
      <span class="badge-dot bg-green"></span>
      <span class="badge-dot bg-blue"></span>
      <span class="badge-dot bg-gray"></span>
    </div>
    <!-- 添加院校管理文字 -->
    <div class="user-management-title">院校管理</div>

    <!-- 筛选和搜索区域 -->
    <div class="content-container">
      <el-row :gutter="24" class="filter-search-row" justify="flex-end">
        <el-col :span="8" class="filter-col">
          <!-- 修改内联样式，将 font-size 设置为合适的值 -->
          <span
              style="color: #000;font-size: 14px;width: 55%;
            text-align: center;line-height: 33px;border: 1px solid #DCDFE6;"
          >学校</span>
          <el-select
              v-model="select"
              placeholder="输入选择"
              class="custom-select"
          >
            <el-option label="请选择" value="1"></el-option>
            <el-option label="南昌大学" value="2"></el-option>
            <!-- 可根据实际情况添加更多院校选项 -->
          </el-select>
        </el-col>
        <el-col :span="12" class="search-col">
          <el-input
              v-model="search"
              placeholder="请输入院校名称"
              class="search-input"
          >
            <template #append>
              <el-button icon="el-icon-search" @click="searchSchools">搜索</el-button>
            </template>
          </el-input>
        </el-col>
      </el-row>

      <!-- 院校列表 -->
      <div class="user-list">
        <div class="list-header">
          <span>院校列表</span>
          <div class="sort-buttons">
            <el-button type="text">时间最新</el-button>
            <el-button type="text">
              <i class="el-icon el-icon-arrow-up" style="margin-left: 4px;"></i>
              编号升序
            </el-button>
            <el-button type="text">
              <i class="el-icon el-icon-arrow-down" style="margin-left: 4px;"></i>
              编号降序
            </el-button>
            <el-button @click="addSchool">添加院校</el-button>
          </div>
        </div>
        <div class="user-cards">
          <div class="card" v-for="(school, index) in schools" :key="index">
            <div class="user-info">
              <span class="user-name">{{ school.name }}</span>
              <el-tag :type="school.status === '正常' ? 'success' : 'danger'">{{ school.status }}</el-tag>
              <span class="register-time">编号：{{ school.id }}</span>
              <span class="balance">添加时间：{{ school.addTime }}</span>
            </div>
            <div class="actions">
              <el-button size="small" round @click="viewSchoolDetails(school)">详情设置</el-button>
            </div>
          </div>
        </div>
      </div>
    </div>

    <!-- 院校信息弹窗 -->
    <el-dialog title="院校信息" :visible.sync="dialogVisible">
      <el-form :model="schoolInfo" ref="schoolInfoForm" label-width="100px">
        <el-form-item label="院校编号">
          <el-input v-model="schoolInfo.id" disabled></el-input>
        </el-form-item>
        <el-form-item label="院校名称">
          <el-input v-model="schoolInfo.name"></el-input>
        </el-form-item>
        <el-form-item label="状态">
          <el-select v-model="schoolInfo.status">
            <el-option label="正常" value="正常"></el-option>
            <el-option label="被限制" value="被限制"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="添加时间">
          <el-input v-model="schoolInfo.addTime"></el-input>
        </el-form-item>
      </el-form>
      <template #footer>
        <el-button @click="dialogVisible = false">取消</el-button>
        <el-button type="success" @click="saveSchoolInfo">保存</el-button>
      </template>
    </el-dialog>

    <!-- 添加院校对话框 -->
    <el-dialog title="添加院校" :visible.sync="addDialogVisible">
      <el-form :model="newSchool" ref="addSchoolForm" label-width="100px">
        <el-form-item label="院校名称">
          <el-input v-model="newSchool.name"></el-input>
        </el-form-item>
        <el-form-item label="院校状态">
          <el-select v-model="newSchool.status">
            <el-option label="正常" value="正常"></el-option>
            <el-option label="被限制" value="被限制"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="院校编号">
          <el-input v-model.number="newSchool.id"></el-input>
        </el-form-item>
        <el-form-item label="添加时间">
          <el-input v-model="newSchool.addTime"></el-input>
        </el-form-item>
      </el-form>
      <template #footer>
        <el-button @click="addDialogVisible = false">取消</el-button>
        <el-button type="success" @click="confirmAddSchool">保存</el-button>
      </template>
    </el-dialog>
  </div>
</template>

<script>
export default {
  name: "adminInstitutionManagement",
  data() {
    return {
      select: '1', // 院校筛选
      search: '', // 搜索关键字
      schools: [
        {
          name: "南昌大学",
          status: "正常",
          id: 1,
          addTime: "2024-12-22 08:38:58"
        },
        {
          name: "南昌大学",
          status: "被限制",
          id: 1,
          addTime: "2024-12-22 08:38:58"
        }
      ],
      schoolInfo: {}, // 当前查看的院校信息
      dialogVisible: false, // 弹窗显示状态
      addDialogVisible: false, // 添加院校对话框显示状态
      newSchool: {
        name: '',
        status: '正常',
        id: null,
        addTime: ''
      }
    };
  },
  methods: {
    searchSchools() {
      // 搜索院校逻辑
      console.log("搜索院校：", this.search);
    },
    addSchool() {
      // 显示添加院校对话框
      this.addDialogVisible = true;
      // 重置新院校信息
      this.newSchool = {
        name: '',
        status: '正常',
        id: null,
        addTime: ''
      };
    },
    confirmAddSchool() {
      // 验证输入信息
      this.$refs.addSchoolForm.validate((valid) => {
        if (valid) {
          // 添加新院校信息到 schools 数组
          this.schools.push({ ...this.newSchool });
          // 关闭添加院校对话框
          this.addDialogVisible = false;
          // 提示添加成功
          this.$message.success('院校添加成功');
        } else {
          // 提示输入信息有误
          this.$message.error('请输入有效的院校信息');
        }
      });
    },
    viewSchoolDetails(school) {
      // 查看院校详情逻辑
      this.schoolInfo = { ...school };
      this.dialogVisible = true;
    },
    saveSchoolInfo() {
      // 保存院校信息逻辑
      // 找到要更新的院校索引
      const index = this.schools.findIndex((s) => s.id === this.schoolInfo.id);
      if (index !== -1) {
        // 更新院校信息
        this.schools[index] = { ...this.schoolInfo };
        // 提示保存成功
        this.$message.success('院校信息保存成功');
      }
      this.dialogVisible = false;
    }
  }
};
</script>

<style scoped>
.user-management {
  padding: 20px;
}

.status-dots {
  display: flex;
  gap: 5px;
  margin-bottom: 10px; /* 减少彩色小球和院校管理文字之间的间距 */
}

.user-management-title {
  font-size: 20px; /* 增大院校管理文字大小 */
  font-weight: normal;
  margin-bottom: 20px; /* 调整院校管理文字和下方内容的间距 */
  margin-left: 60px; /* 增加左边距，右移更多，可按需调整 */
}

.content-container {
  margin-left: 60px; /* 整体右移 */
}

.badge-dot {
  display: inline-block;
  width: 8px;
  height: 8px;
  border-radius: 50%;
}

.bg-red {
  background-color: #FF0000;
}

.bg-yellow {
  background-color: #FFD700;
}

.bg-green {
  background-color: #008000;
}

.bg-blue {
  background-color: #1E90FF;
}

.bg-gray {
  background-color: #808080;
}

.filter-search-row {
  margin-bottom: 20px;
}

.filter-col {
  display: flex;
  align-items: center;
}

.search-col {
  display: flex;
  align-items: center;
}

.search-input {
  width: 300px;
}

.user-list {
  border: 1px solid #dcdfe6;
  padding: 20px;
}

.list-header {
  display: flex;
  justify-content: space-between;
  margin-bottom: 10px;
}

.sort-buttons {
  display: flex;
  gap: 10px;
  align-items: center;
}

.user-cards {
  display: flex;
  flex-direction: column;
  gap: 15px;
}

.card {
  padding: 15px;
  border: 1px solid #dcdfe6;
  border-radius: 5px;
}

.user-info {
  display: flex;
  gap: 10px;
  align-items: center;
  flex-wrap: wrap;
}

.actions {
  margin-top: 10px;
  display: flex;
  gap: 10px;
}

.user-details {
  padding: 10px;
}
</style>